package com.emergency.embackend.service.impl;

import com.emergency.embackend.mapper.HelpDataMapper;
import com.emergency.embackend.mapper.QuizHistoryMapper;
import com.emergency.embackend.mapper.ShareDataMapper;
import com.emergency.embackend.service.HistoryService;
import com.emergency.embackend.service.ShareDataService;
import com.emergency.embackend.utils.RedisConstants;
import com.emergency.embackend.utils.UserHolder;
import com.emergency.embackend.vo.HistoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Set;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ShareDataMapper shareDataMapper;
    @Autowired
    private HelpDataMapper helpDataMapper;
    @Autowired
    private QuizHistoryMapper quizHistoryMapper;
    @Override
    public HistoryVO getDataInWeek() {
        HistoryVO historyVO = new HistoryVO();
        Long id = UserHolder.getUser().getId();
        //从redis中 zset 文章、视频的数据
        String videoKey = RedisConstants.VIEW_VIDEO_HISTORY_KEY+id;
        String articleKey = RedisConstants.VIEW_ARTICLE_HISTORY_KEY+id;
        double currentTime = System.currentTimeMillis();
        double dayTime= 86400000;
        double minTime =currentTime - (dayTime * 7);
        Set<String> videoSet = stringRedisTemplate.opsForZSet().rangeByScore(videoKey, minTime, currentTime);
        if(videoSet ==null){
            historyVO.setVideoNum(0L);
        }
        Set<String> articleSet = stringRedisTemplate.opsForZSet().rangeByScore(articleKey, minTime, currentTime);
        if(articleSet == null){
            historyVO.setArticleNum(0L);
        }
        //从数据库中获取一周内发表的分享、帮助
        Long shareNum =shareDataMapper.getDataInWeek(id);
        if(shareNum==null){
            historyVO.setShareNum(0L);
        }
        //获取求助记录
        Long helpNum = helpDataMapper.getDataInWeek(id);
        if(helpNum == null){
            historyVO.setHelpNum(0L);
        }
        //获取做题记录
        Long quizNum = quizHistoryMapper.getDataInWeek(id);
        if(quizNum == null){
            historyVO.setQuizNum(0L);
        }
        historyVO.setQuizNum(quizNum);
        historyVO.setShareNum(shareNum);
        historyVO.setArticleNum(Long.valueOf(articleSet.size()));
        historyVO.setHelpNum(helpNum);
        historyVO.setVideoNum(Long.valueOf(videoSet.size()));
        return historyVO;
    }

    @Override
    public HistoryVO getDataInMonth() {
        HistoryVO historyVO = new HistoryVO();
        Long id = UserHolder.getUser().getId();
        //从redis中 zset 文章、视频的数据
        String videoKey = RedisConstants.VIEW_VIDEO_HISTORY_KEY+id;
        String articleKey = RedisConstants.VIEW_ARTICLE_HISTORY_KEY+id;
        double currentTime = System.currentTimeMillis();
        double dayTime= 86400000;
        double minTime =currentTime - (dayTime * 30);
        Set<String> videoSet = stringRedisTemplate.opsForZSet().rangeByScore(videoKey, minTime, currentTime);
        if(videoSet ==null){
            historyVO.setVideoNum(0L);
        }
        Set<String> articleSet = stringRedisTemplate.opsForZSet().rangeByScore(articleKey, minTime, currentTime);
        if(articleSet == null){
            historyVO.setArticleNum(0L);
        }
        //从数据库中获取一周内发表的分享、帮助
        Long shareNum =shareDataMapper.getDataInMonth(id);
        if(shareNum==null){
            historyVO.setShareNum(0L);
        }
        //获取求助记录
        Long helpNum = helpDataMapper.getDataInMonth(id);
        if(helpNum == null){
            historyVO.setHelpNum(0L);
        }
        //获取做题记录
        Long quizNum = quizHistoryMapper.getDataInMonth(id);
        if(quizNum == null){
            historyVO.setQuizNum(0L);
        }
        historyVO.setQuizNum(quizNum);
        historyVO.setShareNum(shareNum);
        historyVO.setArticleNum(Long.valueOf(articleSet.size()));
        historyVO.setHelpNum(helpNum);
        historyVO.setVideoNum(Long.valueOf(videoSet.size()));
        return historyVO;
    }
}
